
#define _SYSTEMVAR
#include "public.h"
const puchar SettingVar_Table[STORE_VARIABLE_NUMBER]=
{
 ///SAVE IN EEPROM
 	&nEQ_Mode,
	&nBass_Value,
	&nTreble_Value,
	&nSubwoofer_Value,
	&nLoud_Value,
	&nFade_Value,		
	&nBalance_Value,///7
	
	&asp_setting[0],
	&asp_setting[1],
	&asp_setting[2],
	&asp_setting[3],
	&asp_setting[4],
 	

	&(uchar)Password_Setting.byte,
	&(uchar)Setting_Flag.byte,
#if 0	
 	&MenuPicture,///15
	&MenuColor,
	&MenuContrast,		
	&MenuBright,
	&MenuTint,
#endif
  	&AUD_BT_Level,///20
 	&AUD_Navi_Level,
 	&AUD_TA_Level,
 	&TurnOn_Volume,	// nVolume,
 	&(uchar)FrontSource,
	&(uchar)RearSource,

#if 0///TV_FUNCTION
 	&TV_fromat,
 #endif	
///	&Channel_Gain[0],
///	&Channel_Gain[1],
///	&Channel_Gain[2],
///	&Channel_Gain[3],
///	&Channel_Gain[4],
///	&Channel_Gain[5],
///	&Channel_Gain[6],
///	&Channel_Gain[7],	
///	&Channel_Gain[8],
///	&Channel_Gain[9],
///	&Channel_Gain[10],

 ///in eeprom 
 	&(uchar)Radio.Area,
 	&(uchar)RDS_Flag.byte,
 ///SAVE IN RAM
///	&MPEG_SEPARATE_VOLUME[0],	
///	&MPEG_SEPARATE_VOLUME[1],
///	&MPEG_SEPARATE_VOLUME[2],///20
///	&MPEG_SEPARATE_VOLUME[3],
///	&MPEG_SEPARATE_VOLUME[4],
///	&MPEG_SEPARATE_VOLUME[5],
///	&MPEG_SEPARATE_VOLUME[6],
	&volume_mem,
	&MainVolLevel[1],
	&(uchar)Navi_Osd_Language,
	
	
};

const uchar Var_Range_Table[STORE_VARIABLE_NUMBER][3]=
{
	//min,	max,	default
	
	{MIN_EQ_MODE,MAX_EQ_MODE,DEFAULT_EQ_MODE},
	{MIN_BASS,MAX_BASS,DEFAULT_BASS},
	{MIN_TREBLE,MAX_TREBLE,DEFAULT_TREBLE},
	{MIN_SUBWOOFER,MAX_SUBWOOFER,DEFAULT_SUBWOOFER},
	{MIN_LOUD,MAX_LOUD,DEFAULT_LOUD},
	{MIN_FADE,MAX_FADE,DEFAULT_FADE},		
	{MIN_BALANCE,MAX_BALANCE,DEFAULT_BALANCE},

	{C_FREQ_60HZ,C_FREQ_200HZ,C_FREQ_60HZ},
	{C_FREQ_10K,C_FREQ_17_5K,C_FREQ_15K},
	{FILTER_80HZ,FILTER_160HZ,FILTER_80HZ},
	{MIX_FUNCTION_ENABLE,	 MIX_FUNCTION_DISABLE,    MIX_FUNCTION_ENABLE},	
	{BASS_Q_1,BASS_Q_2,BASS_Q_1_50},
	
	{0x00,0XFF,0x00},
	{0x00,0xff,DFT_SETTING_FLAG},
#if 0
	{0,ADJUST_STEPS,DEFAULT_STEP},
	{0,ADJUST_STEPS,DEFAULT_STEP},
	{0,ADJUST_STEPS,DEFAULT_STEP},		
	{0,ADJUST_STEPS,DEFAULT_STEP},
	{0,ADJUST_STEPS,DEFAULT_STEP},
#endif
	{0,40,10},
	{0,40,20},///{0,40,10},
	{0,40,20},
	{MIN_VOLUME,MAX_VOLUME,DEFAULT_VOLUME},
	{SOURCE_TUNER,SOURCE_WB,SOURCE_TUNER},
	{SOURCE_DVD,SOURCE_EMPTY,SOURCE_EMPTY},

#if 0///TV_FUNCTION
 	{TV_PAL_BG,TV_NTSC_MN,TV_PAL_DK},
 #endif	
	///{0,14,7},
	///{0,14,7},
	///{0,14,7},
	///{0,14,7},
	///{0,14,7},
	///{0,14,7},
	///{0,14,7},
	///{0,14,7},
	///{0,14,7},
	///{0,14,7},
	///{0,14,7},
	////{0,0xe7,0x43},	//TFT_Direct_Angle
	{AREA_USA,AREA_JAPAN,DEFAULT_REGION},
	{0,0xff,RDS_SETTING_FLAG},
///	{MIN_6CH_VOLUME,MAX_6CH_VOLUME,DEFAULT_6CH_VOLUME},	
///	{MIN_6CH_VOLUME,MAX_6CH_VOLUME,DEFAULT_6CH_VOLUME},
///	{MIN_6CH_VOLUME,MAX_6CH_VOLUME,DEFAULT_6CH_VOLUME},
///	{MIN_6CH_VOLUME,MAX_6CH_VOLUME,DEFAULT_6CH_VOLUME},
///	{MIN_6CH_VOLUME,MAX_6CH_VOLUME,DEFAULT_6CH_VOLUME},
///	{MIN_6CH_VOLUME,MAX_6CH_VOLUME,DEFAULT_6CH_VOLUME},
	{MIN_VOLUME,MAX_VOLUME,DEFAULT_VOLUME},
	{MIN_VOLUME,MAX_VOLUME,DEFAULT_VOLUME},
	{OSD_INDEX_EN,OSD_INDEX_ALL,OSD_INDEX_EN},
 	

};
///uchar buff_SYS[STORE_VARIABLE_NUMBER+1];
#pragma section (fcode)

/////////////////////////////////////////////////////////////////////////////
// Function name		: DefaultSetting
// Purpose			: 
// Input Parameters	: none.
// Return type		: none.
// Output Parameters: none.
// Remarks          		:?
/////////////////////////////////////////////////////////////////////////////
void DefaultSetting(void)
{
#if 1
	uchar index=0;

	///for(index=STORE_VARIABLE_NUMBER-1;index>0;index--)
	for(index=0;index<STORE_VARIABLE_NUMBER;index++)
	{
		*SettingVar_Table[index]=Var_Range_Table[index][DEFAULT_VALUE];
	}
#endif
 
}
/////////////////////////////////////////////////////////////////////////////
// Function name		: VerifySetting
// Purpose			: 
// Input Parameters	: none.
// Return type		: none.
// Output Parameters: none.
// Remarks          		:?
/////////////////////////////////////////////////////////////////////////////
void VerifySetting(void)
{
	uchar index=0;
	puchar pVar=0;

	for(index=0;index<STORE_VARIABLE_NUMBER;index++)
	{
		pVar=SettingVar_Table[index];
		if(*pVar>Var_Range_Table[index][MAX_VALUE]||*pVar<Var_Range_Table[index][MIN_VALUE])
			*pVar=Var_Range_Table[index][DEFAULT_VALUE];

	}
}
/////////////////////////////////////////////////////////////////////////////
// Function name		: SaveSetting
// Purpose			: 
// Input Parameters	: none.
// Return type		: none.
// Output Parameters: none.
// Remarks          		:?
/////////////////////////////////////////////////////////////////////////////
void SaveSetting(void)
{
	uchar i;
///	if(!Is_Machine_Power) return;
	VerifySetting();
	for(i=0;i<STORE_VARIABLE_NUMBER;i++)
	{
		buff_array[i]=*SettingVar_Table[i];
	}
	CLEAR_IDWG;	
#ifdef D_STM8_INNEREEPROM
    WriteSettingEeprom();
#else
	SaveSettingEeprom(buff_array,STORE_VARIABLE_NUMBER,USER_DATA_ADDR) ;
#endif
}
/////////////////////////////////////////////////////////////////////////////
// Function name		: LoadSetting
// Purpose			: 
// Input Parameters	: none.
// Return type		: none.
// Output Parameters: none.
// Remarks          		:?
/////////////////////////////////////////////////////////////////////////////
 void LoadSetting(void)
{
	uchar Index;
	bool bResult=false;
#ifdef D_STM8_INNEREEPROM
    bResult=ReadSettingEeprom();
#else
	//bResult=LoadFromEeprom(buff,STORE_VARIABLE_NUMBER,USER_DATA_ADDR1);
	bResult=LoadFromEeprom(buff_array,STORE_VARIABLE_NUMBER,USER_DATA_ADDR);

	if(bResult==false) 
	{
		///bResult=LoadFromEeprom(buff,STORE_VARIABLE_NUMBER+1,USER_DATA_ADDR);	
		bResult=LoadFromEeprom(buff_array,STORE_VARIABLE_NUMBER,USER_DATA_ADDR);	
	}		
#endif
	if(bResult==false)
	{
		//DefaultSetting(SETTING_DATA_NUMBER);
		DefaultSetting();
		///SaveSetting();
	}
	else
	{
		for(Index=0;Index<STORE_VARIABLE_NUMBER;Index++)
		{
			*SettingVar_Table[Index]=buff_array[Index];
		}
		VerifySetting();
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function name		: Generate_CheckSum
// Purpose			: 
// Input Parameters	: none.
// Return type		: none.
// Output Parameters: none.
// Remarks          		:?
/////////////////////////////////////////////////////////////////////////////
void Generate_CheckSum(void)
{

	if(bMaking==false)
	{
		bMaking=true;
		CheckHead=0x55;
		CheckSum=ResetSource;
		
		
		CheckSum+=Clock.sec;
		CheckSum+=Clock.min;
		CheckSum+=Clock.hour;
		CheckSum+=Clock.date;
		CheckSum+=Clock.month;
		CheckSum+=Clock.year;

		CheckSum=~CheckSum+1;
		CheckTail=0xaa;
	}
	bMaking=false;

}
 bool Check_CheckSum(void)
{
	if(CheckHead!=0x55)
		return false;
	if(CheckTail!=0xaa)
		return false;
	CheckSum+=ResetSource;
	CheckSum+=Clock.sec;
	CheckSum+=Clock.min;
	CheckSum+=Clock.hour;
	CheckSum+=Clock.date;
	CheckSum+=Clock.month;
	CheckSum+=Clock.year;
	
	if(CheckSum!=0)
		return false;

	return true;
}

#pragma section ()

